package cn.windy.samary.module.admin.controller;

import cn.windy.samary.module.admin.common.UserOnline;
import cn.windy.samary.module.admin.service.ISessionService;
import cn.windy.samary.utils.PageContent;
import cn.windy.samary.utils.Res;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyl
 * @date 2018-11-08 10:08
 */
@Controller
@RequestMapping("admin/online")
public class OnlineController {

    @Resource
    private ISessionService sessionService;

    @GetMapping
    public String index(Model model) {
        return "admin/online/online";
    }

    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> list(PageContent pageContent) {
        int offset = pageContent.getOffset();
        int limit = pageContent.getLimit();
        List<UserOnline> list = sessionService.list();
        List<UserOnline> list2 = new ArrayList<>();
        for (int i = offset; i < ((offset + limit) <= list.size() ? offset + limit : list.size()); i++) {
            list2.add(list.get(i));
        }
        return Res.rows(list2, (long) list.size());
    }

    @PostMapping("forceLogout/{id}")
    @ResponseBody
    public Map<String, Object> forceLogout(@PathVariable("id") String id) {
        return sessionService.forceLogout(id) ? Res.ok() : Res.error();
    }
}
